<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <title></title>
    <link rel="stylesheet" href="../../layui/css/layui.css">
    <link rel="stylesheet" href="../../layui/css/detail.css">
</head>

<body class="layui-layout-body">

    <div class="layui-body inner-body">

        <!-- 内容主体区域 -->
        <div style="padding: 15px">

            <fieldset class="layui-elem-field layui-field-title">
                <legend>
                    <button class="layui-btn layui-btn-sm">
                        <a href="../../pages/userManagement.html">返回</a>
                    </button>
                    修改用户
                </legend>
            </fieldset>

            <form class="layui-form" action="" lay-filter="updateForm">
                <div class="layui-form-item layui-form-text">
                    <label class="layui-form-label">手机号</label>
                    <div class="layui-input-block" style="width:500px">
                        <input type="text" name="mobile" required lay-verify="phone" placeholder="请输入手机号" autocomplete="off"
                            class="layui-input">
                    </div>
                </div>
                <label class="layui-form-label">密码</label>
                <div class="layui-input-block" style="width:500px;margin-bottom: 10px;">
                    <input name="pwd" class="layui-input" type="password" placeholder="请输入密码" autocomplete="off"
                        lay-verify="required">
                </div>

                <label class="layui-form-label">管理员</label>
                <input name="isAdmin" type="checkbox" value="true" lay-skin="switch">
                <div class="layui-unselect layui-form-switch layui-form-onswitch" lay-skin="_switch">
                    <em></em><i></i>
                </div>

                <div class="layui-form-item">
                    <div class="layui-input-block">
                        <button type="button" class="layui-btn" lay-submit="" lay-filter="user-addUser">修改用户</button>
                        <button type="reset" class="layui-btn layui-btn-primary">重置</button>
                    </div>
                </div>
            </form>
        </div>
    </div>
    </div>
    </div>
    <script src="../../js/lodash.min.js"></script>
    <script src="../../layui/jquery-1.12.4.min.js"></script>
    <script src="../../layui/layui.js"></script>
    <script>
        function query(url) {
            const parst = url.split("?");
            if (parst.length == 1) {
                return {};
            }
            const obj = {};
            parst[1].split("&").map(item => item.split("=")).forEach(([p, v]) => {
                obj[p] = decodeURIComponent(v);
            })
            return obj;
        }
        const userPage = query(location.href);
        //JavaScript代码区域
        layui.use(['form', "layer"], async function () {
            let form = layui.form;
            //提交

            const userInfo = await $.getJSON("/api/User/" + userPage.id);

            form.val("updateForm", userInfo);
            
            form.on("submit(user-addUser)", function (data) {
                $.ajax({
                    url: "/api/User/" + userPage.id,//该用户的id
                    method: "put",
                    data: data.field
                });

                layer.msg('修改用户成功', {
                    icon: 1,
                    time: 1000
                }, function () {
                    location.href = "../../pages/userManagement.html";
                });
            });
        });

    </script>

</body>

</html>